/*
 * @Description:
 * @Author: cgt
 * @Date: 2021-12-23 14:05:56
 * @LastEditors: cgt
 * @LastEditTime: 2021-12-28 19:47:46
 */
import IThematic from '../IThematic';
import ScanPointObject from '../objects/ScanPointObject';

/**
 * 扫描点图层
 */
class ScanPointLayer extends IThematic {
  constructor(tool, options) {
    super(tool, options || {});
  }

  /**
   * 初始化专题图层
   * @override
   * @ignore
   */
  initLayers() {
    this.layeridPrefix = 'ScanPointLayer';
    this.sourceId = this.layeridPrefix + '_source_' + this.id;
    this.layerId = this.layeridPrefix + '_layer_' + this.id;
    this.layerIdPrefixes = [this.layeridPrefix];
    this.layerIds = [this.layerId];
    this.initScanPointObject();
    this.getMap().addSource(this.sourceId, {
      type: 'geojson',
      data: {
        type: 'FeatureCollection',
        features: []
      }
    });
    this.getMap().addLayer({
      id: this.layerId,
      type: 'symbol',
      source: this.sourceId,
      layout: {
        'icon-image': this.objectId,
        'icon-size': ['get', 'iconSize'],
        'icon-allow-overlap': true,
        'icon-ignore-placement': true,
        'icon-anchor': 'center',
        'text-field': '{' + this.opts.label + '}',
        'text-size': 12,
        'text-font': ['Microsoft YaHei Regular'],
        'text-offset': [0, 1],
        'text-allow-overlap': false,
        'text-ignore-placement': false,
        'text-optional': true
      },
      paint: {
        'text-color': ['get', 'textColor']
      }
    });
  }

  /**
   * 初始化扫描点
   */
  initScanPointObject() {
    this.objectId = 'SCAN_POINT_OBJECT_ID';
    if (!this.getMap().hasImage(this.objectId)) {
      this.getMap().addImage(this.objectId, new ScanPointObject(200, this.getMap()), { pixelRatio: 2 });
    }
  }
}

export default ScanPointLayer;
